fix: Fix variable assign num not working#2427
Conversation
--bug=1052497 --user=刘瑞斌 【变量赋值】-将全局变量赋值为num类型,输出时报错 https://www.tapd.cn/57709429/s/1659733
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| result['output_value'] = val | ||
| else: | ||
| reference = self.get_reference_content(variable['reference']) | ||
| self.workflow_manage.context[variable['fields'][1]] = reference |
There was a problem hiding this comment.
The provided code snippet appears to be a function within a class called execute. It's designed to handle different types of variables during execution. Here are a few comments on the code:
-
Empty Default Case: The commented-out
elseblock after checking if the type is'string'suggests that there might have been a default case intended, but it's currently not used. Consider removing this commented-out block or making it functional. -
Type Check for 'string' Only: Since the condition checks only for
'str', all other types (likedictor any custom object) will fall into the defaultifblock or remain unchanged by the current logic. This can lead to potential issues when trying to process complex variable values. -
Variable Value Assignment: Assigning both
result['output_value']andvariable['value']inside the same condition might not be necessary unless specifically required in the context of some operation you're missing from the comment above. -
Optimization Suggestions:
- Avoid Repeated Context Updates: Ensure that updating the workflow context is done efficiently without redundant operations.
- Validate Variable Type Before Parsing: Although your parsing based on type seems correct now, consider adding additional validation before attempting to convert strings to JSON, dictionaries, or other objects.
Here’s an improved version with these considerations taken into account:
def execute(self, variable_list, stream, **kwargs):
result = {'output_value': ''}
for variable in variable_list:
if variable['type'].lower() == 'json':
try:
val = json.loads(variable['value'])
except (JSONDecodeError, TypeError):
print(f"Failed to decode JSON: {variable['value']}")
continue
elif variable['type'].upper() == 'STRING':
# Parse string variables using self.workflow_manage.generate_prompt
val = self.workflow_manage.generate_prompt(variable['value'])
# Assuming no conversion needed for scalar primitives (int, float)
else:
val = variable['value']
self.workflow_manage.context[variable['fields'][1]] = val
# Optionally, update output value
result['output_value'] = val
return NodeResult(result=result, status_code=0) # Add appropriate status codesThis revision includes type-checking explicitly for "json" and "STRING", uses exceptions to manage JSONDecodeError/TypeError cases gracefully, ensures consistent variable handling, and improves code readability by reducing duplicate assignments and improving conditional structure.
fix: Fix variable assign num not working --bug=1052497 --user=刘瑞斌 【变量赋值】-将全局变量赋值为num类型,输出时报错 https://www.tapd.cn/57709429/s/1659733